//
//  EditInptutView.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/17.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "YOEditInptutView.h"
#import "UIImageView+Extension.h"

#define Start_X 24.0f*KWIDTH_6PLUS_SCALE           // 第一个按钮的X坐标
#define Start_Y 14.0f*KHEIGHT_6PLUS_SCALE          // 第一个按钮的Y坐标
#define Width_Space 45.0f*KWIDTH_6PLUS_SCALE       // 2个按钮之间的横间距
#define Height_Space 13.0f*KHEIGHT_6PLUS_SCALE     // 竖间距
#define Button_Height  30*KHEIGHT_6PLUS_SCALE      // 高
#define Button_Width (kGScreenWidth - 138.0f*KWIDTH_6PLUS_SCALE)/3     // 宽

@interface YOEditInptutView ()<UITextViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIView *view_cover;//遮罩层
@property (nonatomic,strong) UIImageView *view_showInputView;//展示输入内容的背景
@property (nonatomic,strong) UILabel *lable_title;//提示语
@property (nonatomic,strong) UITextView *textView_input;
@property (nonatomic,strong) UILabel *numLab;//提示剩下输入的字数

@property (nonatomic,strong) UIButton *titleBtn;//情感状态按钮

@property (nonatomic,strong) UITextField *goldTF;//自定义金币
@property (nonatomic,strong) UILabel *goldLb;//自定义金币
@property (nonatomic,strong) NSString *differenceString;//区分
@property (nonatomic,strong) NSString *key;//兑换标识

@property (nonatomic,strong) UITextField *InvitationCodeTF;//邀请码输入框
@property (nonatomic,assign) NSInteger textLength;
@property(nonatomic, weak) UIImageView *gifView;

@end

@implementation YOEditInptutView

/**
 *输入内容 界面布局 传值
 */
- (id) initWithFrame:(CGRect)frame andPlaceholder:(NSString *)placeholder WithPlcText:(NSString *)plcText WithTextLength:(NSInteger)textLength {
    if (self = [super initWithFrame:frame]) {
        [self initWithAllSubviewsPlaceholder:placeholder WithPlcText:plcText WithTextLength:textLength];
    }
    return self;
}

- (void) initWithAllSubviewsPlaceholder:(NSString *)placeholder WithPlcText:(NSString *)plcText WithTextLength:(NSInteger)textLength {
    self.textLength = textLength;
    //遮罩层
    self.view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:self.view_cover];
    self.view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //展示输入内容
    self.view_showInputView = [[UIImageView alloc] initWithFrame:CGRectMake(30 * KWIDTH_6PLUS_SCALE, 140 * KHEIGHT_6PLUS_SCALE, kGScreenWidth - 60*KWIDTH_6PLUS_SCALE, 220*KHEIGHT_6PLUS_SCALE)];
    self.view_showInputView.image = [UIImage imageNamed:@"main_edit_bg"];
    self.view_showInputView.userInteractionEnabled = YES;
    self.view_showInputView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.view_showInputView];
    
    //删除按钮
    UIButton *deletedBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view_showInputView.width - 40*KWIDTH_6PLUS_SCALE, 4*KHEIGHT_6PLUS_SCALE, 30*KWIDTH_6PLUS_SCALE, 30*KWIDTH_6PLUS_SCALE)];
    [self.view_showInputView addSubview:deletedBtn];
    [deletedBtn setImage:[UIImage imageNamed:@"main_edit_deleted"] forState:0];
    [deletedBtn addTarget:self action:@selector(dismissViewAction) forControlEvents:UIControlEventTouchUpInside];

    //输入内容的框
    self.textView_input = [[UITextView alloc] initWithFrame:CGRectMake(20*KWIDTH_6PLUS_SCALE, 0, self.view_showInputView.width-40*KWIDTH_6PLUS_SCALE, 82*KHEIGHT_6PLUS_SCALE)];
    self.textView_input.top = deletedBtn.bottom+3*KHEIGHT_6PLUS_SCALE;
    [self.view_showInputView addSubview:self.textView_input];
    self.textView_input.delegate = self;
    self.textView_input.backgroundColor = [UIColor whiteColor];
    self.textView_input.layer.masksToBounds = YES;
    self.textView_input.layer.cornerRadius = 5*KHEIGHT_6PLUS_SCALE;
    self.textView_input.text = placeholder;
    self.textView_input.font = kFont15;
    
    //提示语
    self.lable_title = [[UILabel alloc]init];
    [self.textView_input addSubview:self.lable_title];
    [self.lable_title setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont15 withText:plcText withTextColor:RGB(221, 222, 222) withTextAlignment:(NSTextAlignmentLeft)];
    CGSize size1 = [self.lable_title sizeToFitText:self.lable_title.text withFont:self.lable_title.font];
    self.lable_title.frame = CGRectMake(6*KWIDTH_6PLUS_SCALE, 6*KHEIGHT_6PLUS_SCALE, size1.width, size1.height);

    //限制语
    self.numLab = [[UILabel alloc]init];
    [self.view_showInputView addSubview:self.numLab];
    [self.numLab setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont12 withText:[NSString stringWithFormat:@"剩余输入%ld个字",textLength - placeholder.length] withTextColor:RGB(181, 181, 181) withTextAlignment:(NSTextAlignmentLeft)];
    CGSize size2 = [self.numLab sizeToFitText:self.numLab.text withFont:self.numLab.font];
    self.numLab.frame = CGRectMake(20*KWIDTH_6PLUS_SCALE, self.textView_input.bottom + 8*KHEIGHT_6PLUS_SCALE, self.view_showInputView.width - 32*KWIDTH_6PLUS_SCALE, size2.height);
    if (![placeholder isEqualToString:@""]) {
        self.lable_title.hidden = YES;
    }
    
    //按钮
    UIButton *button_sure = [[UIButton alloc] initWithFrame:CGRectMake(45*KWIDTH_6PLUS_SCALE, self.numLab.bottom + 20*KHEIGHT_6PLUS_SCALE, self.view_showInputView.width - 90*KWIDTH_6PLUS_SCALE, 30*KHEIGHT_6PLUS_SCALE)];
    [self.view_showInputView addSubview:button_sure];
    [button_sure setBackgroundImage:[UIImage imageNamed:@"main_ok_btn"] forState:(UIControlStateNormal)];
    [button_sure setTitle:@"确  定" forState:0];
    [button_sure.titleLabel setFont:kFont16];
    [button_sure addTarget:self action:@selector(okOneAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.view_showInputView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.view_showInputView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
}

//确定按钮
- (void)okOneAction {
    if ([self.textView_input.text isEqualToString:@""]) {
        return;
    }
    if (self.removeView) {
        self.removeView(self.textView_input.text);
    }
}

//移除视图
- (void) dismissViewAction {
    if (self.removeView) {
        [self removeFromSuperview];
    }
}

/**
 *情感状态弹出框 页面布局 传值
 */
- (id) initWithFrame:(CGRect)frame WithLoveStr:(NSString *)loveStr {
    if (self = [super initWithFrame:frame]) {
        [self initViesLoveStr:loveStr];
    }
    return self;
}

- (void)initViesLoveStr:(NSString *)loveStr {
    //遮罩层
    self.view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:self.view_cover];
    self.view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //手势移除
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissViewActionQ)];
    [self.view_cover addGestureRecognizer:gesture];
    
    //展示输入内容
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, kGScreenHeight, kGScreenWidth, 100*KHEIGHT_6PLUS_SCALE)];
    [self addSubview:backView];
    backView.backgroundColor = RGB(245, 245, 245);
    
    NSArray *array = [NSArray arrayWithObjects:@"保密",@"单身",@"恋爱",@"G/L",@"离异",@"已婚",nil];
    for (int i = 0 ; i < array.count; i++) {
        NSInteger index = i % 3;
        NSInteger page = i / 3;
        self.titleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.titleBtn.tag = 10291+i;
        [self.titleBtn setTitle:array[i] forState:(UIControlStateNormal)];
        self.titleBtn.titleLabel.font = kFont13;
        self.titleBtn.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
        [self.titleBtn setButtonLayerCornerRadius:5 BorderWidth:0 BorderColor:nil];
        [self.titleBtn addTarget:self action:@selector(dismissView:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.titleBtn setTitleColor:TEXT_COLOR forState:(UIControlStateNormal)];
        [self.titleBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        [self.titleBtn setBackgroundImage:[UIImage imageNamed:@"main_love_nomal"] forState:(UIControlStateNormal)];
        [self.titleBtn setBackgroundImage:[UIImage imageNamed:@"main_love_selected"] forState:(UIControlStateSelected)];
        [backView addSubview:self.titleBtn];
        
        if ([self.titleBtn.titleLabel.text isEqualToString:loveStr]) {
            self.titleBtn.selected = YES;
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        backView.frame = CGRectMake(0, kGScreenHeight - 100*KHEIGHT_6PLUS_SCALE, kGScreenWidth, 100*KHEIGHT_6PLUS_SCALE);
    }];
}

//移除视图
- (void) dismissView:(UIButton *)sender {
    for (int i = 0; i < 6; i ++) {
        if (sender.tag == 10291 + i) {
            sender.selected = YES;
            continue;
        }
        UIButton *but = (UIButton *)[self viewWithTag:i + 10291];
        but.selected = NO;
    }
}

- (void)dismissViewActionQ {
    if (self.removeView) {
        UIButton *but1 = (UIButton *)[self viewWithTag:10291];
        UIButton *but2 = (UIButton *)[self viewWithTag:10292];
        UIButton *but3 = (UIButton *)[self viewWithTag:10293];
        UIButton *but4 = (UIButton *)[self viewWithTag:10294];
        UIButton *but5 = (UIButton *)[self viewWithTag:10295];
        UIButton *but6 = (UIButton *)[self viewWithTag:10296];
        if (but1.selected == YES) {
            self.removeView(but1.titleLabel.text);
        }else if (but2.selected == YES) {
            self.removeView(but2.titleLabel.text);
        }else if (but3.selected == YES) {
            self.removeView(but3.titleLabel.text);
        }else if (but4.selected == YES) {
            self.removeView(but4.titleLabel.text);
        }else if (but5.selected == YES) {
            self.removeView(but5.titleLabel.text);
        }else {
            self.removeView(but6.titleLabel.text);
        }
    }
}

/**
 *国籍选择弹出框 页面布局 传值
 */
- (id) initWithFrame:(CGRect)frame WithSelectedStr:(NSString *)selectedStr WithArray:(NSArray *)array WithPrompt:(BOOL)prompt {
    if (self = [super initWithFrame:frame]) {
        [self initNationalityViews:selectedStr WithArray:array WithPrompt:prompt];
    }
    return self;
}

- (void)initNationalityViews:(NSString *)selectedStr WithArray:(NSArray *)array WithPrompt:(BOOL)prompt{
    //遮罩层
    self.view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:self.view_cover];
    self.view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //展示输入内容
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 64 + 55*KHEIGHT_6PLUS_SCALE, kGScreenWidth, 50*KHEIGHT_6PLUS_SCALE * array.count)];
    [self addSubview:backView];
    backView.backgroundColor = RGB(245, 245, 245);
    
    for (int i = 0 ; i < array.count; i++) {
        self.titleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.titleBtn.tag = 10341+i;
        [self.titleBtn setTitle:array[i] forState:(UIControlStateNormal)];
        self.titleBtn.titleLabel.font = kFont13;
        [self.titleBtn setTitleColor:TEXT_COLOR forState:(UIControlStateNormal)];
        [self.titleBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        self.titleBtn.frame = CGRectMake(0, (50+1)*i*KHEIGHT_6PLUS_SCALE, kGScreenWidth, 50*KHEIGHT_6PLUS_SCALE);
        [self.titleBtn addTarget:self action:@selector(dismissViewNationality:) forControlEvents:(UIControlEventTouchUpInside)];
        self.titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.titleBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10*KWIDTH_6PLUS_SCALE, 0, self.titleBtn.imageView.image.size.width)];
        [self.titleBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] andSize:CGSizeMake(self.titleBtn.width, self.titleBtn.height)] forState:(UIControlStateNormal)];
        [self.titleBtn setBackgroundImage:[UIImage imageWithColor:NAV_COLOR_RED andSize:CGSizeMake(self.titleBtn.width, self.titleBtn.height)] forState:(UIControlStateSelected)];
        if ([self.titleBtn.titleLabel.text isEqualToString:selectedStr]) {
            self.titleBtn.selected = YES;
        }
        if (prompt == YES) {
            if (self.titleBtn.tag == 10341) {
                self.titleBtn.userInteractionEnabled = NO;
            }
        }
        [backView addSubview:self.titleBtn];
    }
}

//移除视图
- (void) dismissViewNationality:(UIButton *)sender {
    if (self.removeView) {
        if (sender.tag) {
            self.removeView(sender.titleLabel.text);
        }
    }
}

#pragma mark - textview delegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView == self.textView_input) {
        if (textView.text.length>self.textLength) {
            textView.text = [textView.text substringToIndex:self.textLength];
        }
    }
    _numLab.text= [NSString stringWithFormat:@"剩余输入%ld个字",self.textLength - textView.text.length];
    self.lable_title.hidden = (textView.text.length > 0);
}

/**
 *自定义金币
 */
- (id)initWithFrame:(CGRect)frame WithBalanceInt:(NSInteger)balanceInt WithTitleStr:(NSString *)titleStr WithDifferenceStr:(NSString *)differenceStr WithDifferenceTwoStr:(NSString *)differenceTwoStr WithKey:(NSString *)key {
    if (self = [super initWithFrame:frame]) {
        [self initGoldViewsGoldIntWithBalanceInt:balanceInt WithTitleStr:titleStr WithDifferenceStr:differenceStr WithDifferenceTwoStr:differenceTwoStr WithKey:key];
    }
    return self;
}

- (void)initGoldViewsGoldIntWithBalanceInt:(NSInteger)balanceInt WithTitleStr:(NSString *)titleStr WithDifferenceStr:(NSString *)differenceStr WithDifferenceTwoStr:(NSString *)differenceTwoStr WithKey:(NSString *)key {
    self.differenceString = differenceTwoStr;
    self.key = key;
    //遮罩层
    self.view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:self.view_cover];
    self.view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //手势移除
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noGoldBtnAction)];
    [self.view_cover addGestureRecognizer:gesture];
    
    //展示输入内容
    self.view_showInputView = [[UIImageView alloc] initWithFrame:CGRectMake(36*KWIDTH_6PLUS_SCALE, 140*KHEIGHT_6PLUS_SCALE, kGScreenWidth-36*KWIDTH_6PLUS_SCALE*2, 214*KHEIGHT_6PLUS_SCALE)];
    [self addSubview:self.view_showInputView];
    self.view_showInputView.backgroundColor = [UIColor whiteColor];
    self.view_showInputView.layer.cornerRadius = 10.0;
    
    //提示语
    UILabel *oneLb = [[UILabel alloc]init];
    [self.view_showInputView addSubview:oneLb];
    [oneLb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont15 withText:titleStr withTextColor:NAV_COLOR_RED withTextAlignment:(NSTextAlignmentCenter)];
    oneLb.frame = CGRectMake(20*KWIDTH_6PLUS_SCALE, 30*KHEIGHT_6PLUS_SCALE, self.view_showInputView.width - 40*KWIDTH_6PLUS_SCALE, 15*KHEIGHT_6PLUS_SCALE);
    
    //余额
    UILabel *twoLb = [[UILabel alloc]init];
    [self.view_showInputView addSubview:twoLb];
    [twoLb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont14 withText:[NSString stringWithFormat:@"账户:%ld%@",balanceInt,differenceStr] withTextColor:TEXT_COLOR withTextAlignment:(NSTextAlignmentCenter)];
    twoLb.frame = CGRectMake(20*KWIDTH_6PLUS_SCALE, 0, self.view_showInputView.width - 40*KWIDTH_6PLUS_SCALE, 15*KHEIGHT_6PLUS_SCALE);
    twoLb.top = oneLb.bottom + 12*KHEIGHT_6PLUS_SCALE;
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(12*KWIDTH_6PLUS_SCALE, 0, self.view_showInputView.width-24*KWIDTH_6PLUS_SCALE, 30*KHEIGHT_6PLUS_SCALE)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.top = twoLb.bottom + 30*KHEIGHT_6PLUS_SCALE;
    [self.view_showInputView addSubview:bgView];
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderWidth = 1.0;
    bgView.layer.borderColor = NAV_COLOR_RED.CGColor;

    self.goldTF = [[UITextField alloc]initWithFrame:CGRectMake(10*KWIDTH_6PLUS_SCALE, 2*KHEIGHT_6PLUS_SCALE, bgView.width/2 - 30*KWIDTH_6PLUS_SCALE, 26*KHEIGHT_6PLUS_SCALE)];
    self.goldTF.font = kFont12;
    self.goldTF.delegate = self;
    self.goldTF.placeholder = [NSString stringWithFormat:@"请输入兑换的%@数",differenceStr];
    self.goldTF.backgroundColor = [UIColor whiteColor];
    [self.goldTF setValue:RGB_VALUE(0x5a5a5a) forKeyPath:@"_placeholderLabel.textColor"];
    [self.goldTF setValue:kFont12 forKeyPath:@"_placeholderLabel.font"];
    [self.goldTF addTarget:self action:@selector(valueChanged:) forControlEvents:(UIControlEventEditingChanged)];
    self.goldTF.keyboardType = UIKeyboardTypeNumberPad;
    [bgView addSubview:self.goldTF];
    
    //余额
    self.goldLb = [[UILabel alloc]init];
    [bgView addSubview:self.goldLb];
    [self.goldLb setLableStyleWithBackgroundColor:[UIColor whiteColor] withFont:kFont14 withText:nil withTextColor:TEXT_COLOR withTextAlignment:(NSTextAlignmentRight)];
    self.goldLb.frame = CGRectMake(bgView.width/2 - 20*KWIDTH_6PLUS_SCALE, 2*KHEIGHT_6PLUS_SCALE, bgView.width/2 + 10*KWIDTH_6PLUS_SCALE, 26*KHEIGHT_6PLUS_SCALE);
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view_showInputView.width, 1)];
    lineView.top = bgView.bottom + 28*KHEIGHT_6PLUS_SCALE;
    [self.view_showInputView addSubview:lineView];
    lineView.backgroundColor = BG_COLOR_GRAY;
    
    UIView *line1View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, self.view_showInputView.height - lineView.bottom)];
    line1View.centerX = lineView.centerX;
    line1View.top = lineView.bottom;
    [self.view_showInputView addSubview:line1View];
    line1View.backgroundColor = BG_COLOR_GRAY;
    
    //取消按钮
    UIButton *deletedBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view_showInputView.width/2, line1View.height)];
    deletedBtn.top = lineView.bottom;
    deletedBtn.right = line1View.left;
    [self.view_showInputView addSubview:deletedBtn];
    [deletedBtn setTitle:@"取消" forState:0];
    [deletedBtn setTitleColor:NAV_COLOR_RED forState:(UIControlStateNormal)];
    deletedBtn.titleLabel.font = kFont14;
    [deletedBtn addTarget:self action:@selector(noGoldBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    //确定按钮
    UIButton *okBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view_showInputView.width/2, line1View.height)];
    okBtn.top = lineView.bottom;
    okBtn.left = line1View.right;
    [self.view_showInputView addSubview:okBtn];
    [okBtn setTitle:@"确定" forState:0];
    [okBtn setTitleColor:NAV_COLOR_RED forState:(UIControlStateNormal)];
    okBtn.titleLabel.font = kFont14;
    [okBtn addTarget:self action:@selector(okGoldBtnAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)valueChanged:(UITextField*)textField {
    if (textField == self.goldTF) {
        if (textField.text.length>9) {
            textField.text = [textField.text substringToIndex:9];
        }
        if ([self.key isEqualToString:@"票兑换友币"]) {
            self.goldLb.text = [NSString stringWithFormat:@"=%.2f%@",[textField.text floatValue]/25,self.differenceString];
            NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:self.goldLb.text];
            NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"="].location + 1, self.goldLb.text.length - 1);
            [noteStr addAttribute:NSForegroundColorAttributeName value:NAV_COLOR_RED range:redRange];
            [self.goldLb setAttributedText:noteStr];
        }else if ([self.key isEqualToString:@"票兑换钻石"]) {
            self.goldLb.text = [NSString stringWithFormat:@"=%.2f%@",[textField.text floatValue]/2.2,self.differenceString];
            NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:self.goldLb.text];
            NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"="].location + 1, self.goldLb.text.length - 1);
            [noteStr addAttribute:NSForegroundColorAttributeName value:NAV_COLOR_RED range:redRange];
            [self.goldLb setAttributedText:noteStr];
        }else if ([self.key isEqualToString:@"友币兑换钻石"]) {
            self.goldLb.text = [NSString stringWithFormat:@"=%.2f%@",[textField.text floatValue]*10,self.differenceString];
            NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:self.goldLb.text];
            NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"="].location + 1, self.goldLb.text.length - 1);
            [noteStr addAttribute:NSForegroundColorAttributeName value:NAV_COLOR_RED range:redRange];
            [self.goldLb setAttributedText:noteStr];
        }
    }
    if (textField.text.length==0) {
        self.goldLb.hidden = YES;
    }else {
        self.goldLb.hidden = NO;
    }
}

//确定
- (void)okGoldBtnAction {
    if (self.goldTF.text.length == 0) {
        return;
    }
    if ([[self.goldTF.text substringToIndex:1] isEqualToString:@"0"]) {
//        [YOToast YOToastShow:@"输入有误,请重新输入"];
        return;
    }
    if (self.removeView) {
        self.removeView(self.goldTF.text);
    }
}

//取消
- (void)noGoldBtnAction {
    if (self.removeView) {
        self.removeView(nil);
    }
}

/**
 *邀请码弹框
 */
- (id)initWithInvitationCodeFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initWithInvitationCode];
    }
    return self;
}

- (void)initWithInvitationCode {
    //遮罩层
    self.view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:self.view_cover];
    self.view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //手势移除
//    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureInvitationCodeTFAction)];
//    [self.view_cover addGestureRecognizer:gesture];
    
    //展示输入内容
    self.view_showInputView = [[UIImageView alloc] initWithFrame:CGRectMake(36*KWIDTH_6PLUS_SCALE, 200*KHEIGHT_6PLUS_SCALE + kGScreenHeight, kGScreenWidth-36*KWIDTH_6PLUS_SCALE*2, 180*KHEIGHT_6PLUS_SCALE)];
    [self addSubview:self.view_showInputView];
    self.view_showInputView.backgroundColor = RGB(245, 245, 245);
    self.view_showInputView.layer.cornerRadius = 10.0;
    
    //删除按钮
    UIButton *deletedBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view_showInputView.width-38*KWIDTH_6PLUS_SCALE, 8*KHEIGHT_6PLUS_SCALE, 24*KWIDTH_6PLUS_SCALE, 24*KWIDTH_6PLUS_SCALE)];
    [self.view_showInputView addSubview:deletedBtn];
    [deletedBtn setImage:[UIImage imageNamed:@"deleted"] forState:0];
    [deletedBtn addTarget:self action:@selector(InvitationCodeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.InvitationCodeTF = [[UITextField alloc]initWithFrame:CGRectMake(10*KWIDTH_6PLUS_SCALE, deletedBtn.bottom + 8*KHEIGHT_6PLUS_SCALE, self.view_showInputView.width - 20*KWIDTH_6PLUS_SCALE, 26*KHEIGHT_6PLUS_SCALE)];
    self.InvitationCodeTF.font = kFont12;
    self.InvitationCodeTF.placeholder = @"请输入邀请码";
    self.InvitationCodeTF.backgroundColor = [UIColor whiteColor];
    [self.InvitationCodeTF setValue:RGB_VALUE(0x5a5a5a) forKeyPath:@"_placeholderLabel.textColor"];
    [self.InvitationCodeTF setValue:kFont12 forKeyPath:@"_placeholderLabel.font"];
    self.InvitationCodeTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10*KWIDTH_6PLUS_SCALE, 0)];
    self.InvitationCodeTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view_showInputView addSubview:self.InvitationCodeTF];
    
    UIButton *button_sure = [[UIButton alloc] initWithFrame:CGRectMake(18*KWIDTH_6PLUS_SCALE, self.InvitationCodeTF.bottom + 40*KHEIGHT_6PLUS_SCALE, self.view_showInputView.width - 36*KWIDTH_6PLUS_SCALE, 32*KHEIGHT_6PLUS_SCALE)];
    [self.view_showInputView addSubview:button_sure];
    button_sure.backgroundColor = NAV_COLOR_RED;
    button_sure.layer.cornerRadius = 4.0;
    [button_sure setTitle:@"确定" forState:0];
    [button_sure.titleLabel setFont:kFont13];
    [button_sure addTarget:self action:@selector(gestureInvitationCodeTFAction) forControlEvents:UIControlEventTouchUpInside];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.view_showInputView.frame = CGRectMake(36*KWIDTH_6PLUS_SCALE, 200*KHEIGHT_6PLUS_SCALE, kGScreenWidth-36*KWIDTH_6PLUS_SCALE*2, 180*KHEIGHT_6PLUS_SCALE);
    }];
}

- (void)gestureInvitationCodeTFAction {
    if (self.removeView) {
        self.removeView(self.InvitationCodeTF.text);
    }
}

- (void)InvitationCodeBtnAction {
    if (self.removeView) {
        self.removeView(@"InvitationCodeBtnAction");
    }
}

/**
 *断网界面
 */
- (id)initWithFrame:(CGRect)frame WithNoDataBackgroundViewTitle:(NSString *)title WithKey:(NSString *)key {
    if (self = [super initWithFrame:frame]) {
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kGScreenWidth, kGScreenHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        if ([key isEqualToString:@"home"]) {
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(kGScreenWidth/2 - 66*KWIDTH_6PLUS_SCALE , 100*KHEIGHT_6PLUS_SCALE, 132*KWIDTH_6PLUS_SCALE, 120*KHEIGHT_6PLUS_SCALE)];
            img.image = [UIImage imageNamed:@"可爱表情哭泣"];
            img.contentMode = UIViewContentModeScaleAspectFill;//不拉伸图片
            [bgView addSubview:img];
            
            UILabel *Lb = [[UILabel alloc]initWithFrame:CGRectMake(10*KWIDTH_6PLUS_SCALE , 0, kGScreenWidth - 20*KWIDTH_6PLUS_SCALE, 15*KHEIGHT_6PLUS_SCALE)];
            Lb.top = img.bottom + 40*KHEIGHT_6PLUS_SCALE;
            [bgView addSubview:Lb];
            [Lb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont15 withText:title withTextColor:TEXT_COLOR withTextAlignment:(NSTextAlignmentCenter)];
        }else {
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(kGScreenWidth/2 - 66*KWIDTH_6PLUS_SCALE , 200*KHEIGHT_6PLUS_SCALE, 132*KWIDTH_6PLUS_SCALE, 120*KHEIGHT_6PLUS_SCALE)];
            img.image = [UIImage imageNamed:@"可爱表情哭泣"];
            img.contentMode = UIViewContentModeScaleAspectFill;//不拉伸图片
            [bgView addSubview:img];
            
            UILabel *Lb = [[UILabel alloc]initWithFrame:CGRectMake(10*KWIDTH_6PLUS_SCALE , 0, kGScreenWidth - 20*KWIDTH_6PLUS_SCALE, 15*KHEIGHT_6PLUS_SCALE)];
            Lb.top = img.bottom + 40*KHEIGHT_6PLUS_SCALE;
            [bgView addSubview:Lb];
            [Lb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont15 withText:title withTextColor:TEXT_COLOR withTextAlignment:(NSTextAlignmentCenter)];
        }
    }
    return self;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.goldTF) {
        return [Utils validateNumber:string];
    }else {
        return YES;
    }
}

- (id)initWithLodingFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.view_cover = [[UIView alloc] initWithFrame:self.frame];
        [self addSubview:self.view_cover];
        self.view_cover.backgroundColor = [UIColor clearColor];
        NSArray *images = @[[UIImage imageNamed:@"loading_0"], [UIImage imageNamed:@"loading_1"], [UIImage imageNamed:@"loading_2"], [UIImage imageNamed:@"loading_3"], [UIImage imageNamed:@"loading_4"], [UIImage imageNamed:@"loading_5"], [UIImage imageNamed:@"loading_7"], [UIImage imageNamed:@"loading_8"], [UIImage imageNamed:@"loading_9"], [UIImage imageNamed:@"loading_10"]];
        UIImageView *gifView = [[UIImageView alloc] init];

        [self.view_cover addSubview:gifView];
//        [gifView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(0);
//            make.centerY.equalTo(kGScreenHeight);
//            make.width.equalTo(@100);
//            make.height.equalTo(@100);
//        }];
        self.gifView = gifView;
        [gifView playGifAnim:images];
    }
    return self;
}

/**
 *新版本更新提醒框
 */
- (id)initWithFrame:(CGRect)frame WithVLevenStr:(NSString *)vLevenStr WithVContentStr:(NSString *)vContentStr {
    if (self = [super initWithFrame:frame]) {
        //遮罩层
        self.view_cover = [[UIView alloc] initWithFrame:self.frame];
        [self addSubview:self.view_cover];
        self.view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        
        //展示输入内容
        self.view_showInputView = [[UIImageView alloc] initWithFrame:CGRectMake(28 * KWIDTH_6PLUS_SCALE, 132 * KHEIGHT_6PLUS_SCALE, kGScreenWidth - 56*KWIDTH_6PLUS_SCALE, 355*KHEIGHT_6PLUS_SCALE)];
        self.view_showInputView.image = [UIImage imageNamed:@"other_v_bg"];
        self.view_showInputView.userInteractionEnabled = YES;
        self.view_showInputView.backgroundColor = [UIColor clearColor];
        self.view_showInputView.contentMode = UIViewContentModeScaleToFill;
        [self.view_cover addSubview:self.view_showInputView];
        
        UIImageView *titleImgView = [[UIImageView alloc] initWithFrame:CGRectMake(112*KWIDTH_6PLUS_SCALE, 153 * KHEIGHT_6PLUS_SCALE, 112*KWIDTH_6PLUS_SCALE, 24*KHEIGHT_6PLUS_SCALE)];
        titleImgView.image = [UIImage imageNamed:@"other_title"];
        titleImgView.backgroundColor = [UIColor clearColor];
        [self.view_showInputView addSubview:titleImgView];
        
        UILabel *vLabel = [[UILabel alloc]initWithFrame:CGRectMake(0 , 0, 65*KWIDTH_6PLUS_SCALE, 20*KHEIGHT_6PLUS_SCALE)];
        vLabel.bottom = titleImgView.bottom;
        vLabel.left = titleImgView.right + 10*KHEIGHT_6PLUS_SCALE;
        [vLabel setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:[UIFont fontWithName:@"Helvetica-Bold" size:14] withText:vLevenStr withTextColor:NAV_COLOR_RED withTextAlignment:(NSTextAlignmentRight)];
        [self.view_showInputView addSubview:vLabel];
        
        UIButton *goBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        goBtn.frame = CGRectMake(65*KWIDTH_6PLUS_SCALE, 295*KHEIGHT_6PLUS_SCALE, self.view_showInputView.width - 155*KWIDTH_6PLUS_SCALE, 40*KHEIGHT_6PLUS_SCALE);
        [goBtn setTitle:@"速速升级" forState:(UIControlStateNormal)];
        goBtn.titleLabel.font = kFont16;
        [goBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
        [goBtn setBackgroundImage:[UIImage imageNamed:@"main_rz_sfz"] forState:(UIControlStateNormal)];
        [goBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view_showInputView addSubview:goBtn];
        
        UIImageView *titleTwoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(90*KWIDTH_6PLUS_SCALE, 153 * KHEIGHT_6PLUS_SCALE, 145*KWIDTH_6PLUS_SCALE, 13*KHEIGHT_6PLUS_SCALE)];
        titleTwoImgView.bottom = goBtn.top - 10*KHEIGHT_6PLUS_SCALE;
        titleTwoImgView.image = [UIImage imageNamed:@"other_two_title"];
        titleTwoImgView.backgroundColor = [UIColor clearColor];
        [self.view_showInputView addSubview:titleTwoImgView];
        
        UILabel *vContentLabel = [[UILabel alloc]init];
        [vContentLabel setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont14 withText:vContentStr withTextColor:TEXT_COLOR withTextAlignment:(NSTextAlignmentLeft)];
        vContentLabel.numberOfLines = 0;
        CGSize size = CGSizeMake(kGScreenWidth - 40*KWIDTH_6PLUS_SCALE, 5000);
        CGSize textSize = [vContentLabel.text boundingRectWithSize:size
                                                                     options:NSStringDrawingTruncatesLastVisibleLine|
                           NSStringDrawingUsesLineFragmentOrigin|
                           NSStringDrawingUsesFontLeading
                                                                  attributes:@{NSFontAttributeName:vContentLabel.font}
                                                                     context:nil].size;
        vContentLabel.frame = CGRectMake(40*KWIDTH_6PLUS_SCALE, 0, self.view_showInputView.width - 105*KWIDTH_6PLUS_SCALE, textSize.height);
        vContentLabel.top = titleImgView.bottom + 15*KHEIGHT_6PLUS_SCALE;
        [self.view_showInputView addSubview:vContentLabel];

        //删除按钮
        UIButton *deletedBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35*KWIDTH_6PLUS_SCALE, 35*KWIDTH_6PLUS_SCALE)];
        deletedBtn.top = self.view_showInputView.bottom + 32*KHEIGHT_6PLUS_SCALE;
        deletedBtn.centerX = self.view_showInputView.centerX;
        [deletedBtn setImage:[UIImage imageNamed:@"other_v_btn"] forState:0];
        [deletedBtn addTarget:self action:@selector(dismissVLevenViewAction) forControlEvents:UIControlEventTouchUpInside];
        
        if (![[UserConfig shareInstance]._updateInfo[@"isForce"] isEqualToString:@"1"]) {
            [self.view_cover addSubview:deletedBtn];
        }
    }
    return self;
}

- (void)buttonAction {
    if (self.block) {
        self.block(self);
    }
}

- (void)addButtonAction:(ButtonBlock)block {
    self.block = block;
}

- (void)dismissVLevenViewAction {
    if (self.removeView) {
        [self removeFromSuperview];
    }
}

@end
